using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.Caching;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace SharePointing.Features.Translation.Common
{
    public struct Lists
    {
        public const string METATAGTYPELIST = "MetaTagType";
        public const string METATAGLIST = "MetaTags";
    }

    public struct CharacterSequences
    {
        public const string LESSTHAN = "&lt;";
        public const string GREATERTHAN = "&gt;";
        public const string QUOTE = "&quot;";
    }

    public struct ContentTypes
    {
        public const string METATAGTYPECONTENTTYPE = "MetaTagType";
        public const string METATAGCONTENTTYPE = "MetaTag";
        public const string ROOTPUBLISHINGCONTENTTYPE = "Page";
    }
    public struct SiteColumns
    {
        public const string METATAGLOOKUPFIELDNAME = "MetaTag";
        public const string METATAGTYPEFIELDNAME = "MetaTagName";
        public const string METATAGVALUEFIELDNAME = "MetaTagValue";
        public const string METATAGPAGETAGS = "MetaTagPageTags";
        public const string METATAGCOLUMNGROUP = "MetaTags Custom Columns";
    }

    public struct CacheKeys
    {
        public const string SITETAGS = "MetaTags_Site";
    }

    public struct MetaTags
    {
        public const string AUTHOR = "author";
        public const string KEYWORDS = "keywords";
        public const string DESCRIPTION = "description";
    }

    public struct Settings
    {
        public const string ASSEMBLYNAME = "Chk.SharePoint.www.Features.MetaTags, Version=1.0.0.0, Culture=neutral, PublicKeyToken=acb8a6951af63277";
        public const string RECEIVERNAME = "Chk.SharePoint.www.Features.MetaTags.MetaTagEventReceiver";

    }

    public partial class Utilities
    {
        public static string GetLookupValue(string input)
        {
            // When you retrieve the value of a lookup field, it is returned as ID;# instead of
            // just the value that we want. This method returns just the portion
            // to the right of the # character
            string _delimiter = "#";
            // get position of the delimiter
            int index = input.IndexOf(@_delimiter);
            input = input.Substring(index + 1);
            return input;

        }

        public static void ClearCache(string cacheKey)
        {
            // Clears the ASP.NET Cache for the MetaTags
            HttpContext.Current.Cache[cacheKey] = null;
        }

        public static String UTF8ByteArrayToString(Byte[] characters)
        {

            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        public static String SerializeObject(object pageTag)
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(object));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, pageTag);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception e) { return null; }
        }

        public static object DeserializeObject(String pXmlizedString)
        {
            XmlSerializer xs = new XmlSerializer(typeof(object));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(CleanCharacterSequences(pXmlizedString)));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (object)xs.Deserialize(memoryStream);
        }

        public static string CleanCharacterSequences(string stringToClean)
        {
            // This is NOT the way to do this but I had to work around it somehow...
            // needed this because the serialized text is changed and will not 
            // deserialize when we pull it back out of the field. SharePoint translates the characters
            stringToClean = stringToClean.Replace(Common.CharacterSequences.GREATERTHAN, ">");
            stringToClean = stringToClean.Replace(Common.CharacterSequences.LESSTHAN, "<");
            stringToClean = stringToClean.Replace(Common.CharacterSequences.QUOTE, "\"");
            return stringToClean;
        }
    }
}
